/******************************************************************************/
/*                                                                            */
/*  Copyright (c) 2009: ngi-central.org                                       */
/*  All rights reserved.                                                      */
/*                                                                            */
/******************************************************************************/


/// \file
/// This file implements the vector geometric primitive.

#ifndef __sgl_vector_h__
#define __sgl_vector_h__

#include <sgl/global_settings.h>
#include <sgl/conditions.h>
#include <sgl/geometric_primitive.h>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/static_assert.hpp>
#include <boost/operators.hpp>
#include <algorithm>

namespace sgl {


/******************************************************************************/
/*                                                                            */
/* vector                                                                     */
/*                                                                            */
/******************************************************************************/

template <
	/// The dimensionality. 
	//!
	//! This defaults to two dimensions.
	size_t D=2,
	/// The element type. 
	//! 
	//! This type defaults to SGL_FPTYPE (which in turn defaults to double), but 
	//! any other numeric type is also suitable.
	typename T=SGL_FPTYPE>
/// A vector in euclidean space.
class vector
	/// A vector is a geometric primitive.
	: public geometric_primitive<D,T>
	, boost::equality_comparable<sgl::vector<D,T>, sgl::vector<D,T>
	, boost::additive<sgl::vector<D,T>, sgl::vector<D,T>
	, boost::multiplicative<sgl::vector<D,T>, T
	> > >
{
	// A vector must have one or more dimensions.
	BOOST_STATIC_ASSERT(D >= 1);

public:
	/// Default constructor.
	//!
	//! Constructs a zero vector. The contents of the values in the vector are
	//! all initialized to zero.
	vector()
	{ 
		for (size_t i=0; i<D; ++i)
			values_[i] = T();
		for (size_t i=0; i<D; ++i)
			SGL_POSTCONDITION(values_[i] == 0,
				"sgl::vector::values_ is not correctly initialized.");
	}

	/// Default constructor for a two-dimensional vector.
	vector(
		/// The first element of the vector.
		T dx,
		/// The second element of the vector.
		T dy)
	{ 
		BOOST_STATIC_ASSERT(D == 2);

		values_[0] = dx;
		values_[1] = dy;
	}

	/// Default constructor for a three-dimensional vector.
	vector(
		/// The first element of the vector.
		T dx,
		/// The second element of the vector.
		T dy,
		/// The third element of the vector.
		T dz)
	{ 
		BOOST_STATIC_ASSERT(D == 3);

		values_[0] = dx;
		values_[1] = dy;
		values_[2] = dz;
	}

	/// Copy constructor.
	vector(
		/// The right hand side.
		sgl::vector<D,T> const& rhs)
	{ 
		for (size_t i=0; i<D; ++i)
			values_[i] = rhs.values_[i];
		for (size_t i=0; i<D; ++i)
			SGL_POSTCONDITION(values_[i] == rhs.values_[i],
				"sgl::vector::values_ is not correctly initialized.");
	}

	/// Copy assignment operator.
	sgl::vector<D,T> & operator = (
		/// The right hand side.
		sgl::vector<D,T> rhs)
	{
		swap(rhs);
		return *this;
	}

	/// Exchanges the elements of two vector objects.
	void swap(
		/// The other vector.
		sgl::vector<D,T> & rhs)
	{
		SGL_PRECONDITION(D == rhs.dimensions(),
			"dimension mismatch.");

		for (size_t i=0; i<D; ++i)
			std::swap(values_[i], rhs.values_[i]);
	}

	/// Indexing operator.
	//!
	//! /return Returns a reference to the element at the specified index.
	T & operator [] (
		/// The index into the vector (zero based) used to access the vector's 
		/// elements.
		size_t index)
	{
		SGL_PRECONDITION(index >= 0,
			"index parameter is out of range.");
		SGL_PRECONDITION(index < D,
			"index parameter is out of range.");

		return values_[index];
	}

	/// Indexing operator.
	//!
	//! /return Returns a const reference to the element at the specified index.
	T const & operator [] (
		/// The index into the vector (zero based) used to access the vector's 
		/// elements.
		size_t index) const
	{
		SGL_PRECONDITION(index >= 0,
			"index parameter is out of range.");
		SGL_PRECONDITION(index < D,
			"index parameter is out of range.");

		return values_[index];
	}

	/// Find the type of the geometric primitive.
	//!
	//! /return The type of the primitive.
	geometric_type type() const
	{
		return sgl::geometric_primitive<D,T>::vector;
	} 

	/// Access the first element of the vector.
	//!
	//! /return Returns a reference to the first element of the vector.
	T & dx()
	{
		BOOST_STATIC_ASSERT(D >= 1);
		return values_[0];
	}

	/// Access the first element of the vector.
	//!
	//! /return Returns a const reference to the first element of the vector.
	T const & dx() const
	{
		BOOST_STATIC_ASSERT(D >= 1);
		return values_[0];
	}

	/// Access the second element of the vector.
	//!
	//! /return Returns a reference to the second element of the vector.
	T & dy()
	{
		BOOST_STATIC_ASSERT(D >= 2);
		return values_[1];
	}

	/// Access the second element of the vector.
	//!
	//! /return Returns a const reference to the second element of the vector.
	T const & dy() const
	{
		BOOST_STATIC_ASSERT(D >= 2);
		return values_[1];
	}

	/// Access the third element of the vector.
	//!
	//! /return Returns a reference to the third element of the vector.
	T & dz()
	{
		BOOST_STATIC_ASSERT(D >= 3);
		return values_[2];
	}

	/// Access the third element of the vector.
	//!
	//! /return Returns a const reference to the third element of the vector.
	T const & dz() const
	{
		BOOST_STATIC_ASSERT(D >= 3);
		return values_[2];
	}

	/// Unary negation.
	//!
	//! A vector is negated by negating all its components. A negated
	//! vector points in the opposite direction.
	//!
	//! \return A negated vector object.
    sgl::vector<D,T> operator - () const
    {
		sgl::vector<D,T> temp(*this);
		std::transform(values_, values_+D, temp.values_, 
			- boost::phoenix::arg_names::arg1);
        return temp;
    }

	/// Equality operator.
	//!
	//! Two vectors are compared for equality by comparing their components in a
	//! pairwise manner. The vectors are equal only if all pairwise components
	//! are equal.
	//!
	//! Through the use of boost::equality as a base class operator != is 
	//! defined as well and implemented in terms of this operator ==.
	//!
	//! \return true if equal, false otherwise.
    bool operator == (
		/// Right hand side.
		sgl::vector<D,T> const& rhs) const
    {
		SGL_PRECONDITION(D == rhs.dimensions(),
			"dimension mismatch.");

		return std::equal(values_, values_+D, rhs.values_);
    }

	/// Add two vectors.
	//!
	//! Two vectors are added by adding their components in a pairwise 
	//! manner.
	//!
	//! Through the use of boost::additive as a base class operator + is 
	//! defined as well and implemented in terms of this operator +=.
	//!
	//! \return A reference to this object.
    sgl::vector<D,T> & operator += (
		/// Right hand side.
		sgl::vector<D,T> const& rhs)
    {
		SGL_PRECONDITION(D == rhs.dimensions(),
			"dimension mismatch.");

		T * i = values_;
		T const* j = rhs.values_;
		for (; i!=values_+D; ++i, ++j)
			(*i) += (*j);

        return *this;
    }

	/// Subtract two vectors.
	//!
	//! Two vectors are subtracted by subtracting their components in a pairwise 
	//! manner.
	//!
	//! Through the use of boost::additive as a base class operator - is 
	//! defined as well and implemented in terms of this operator -=.
	//!
	//! \return A reference to this object.
    sgl::vector<D,T> & operator -= (
		/// Right hand side.
		sgl::vector<D,T> const& rhs)
    {
		SGL_PRECONDITION(D == rhs.dimensions(),
			"dimension mismatch.");

		T * i = values_;
		T const* j = rhs.values_;
		for (; i!=values_+D; ++i, ++j)
			(*i) -= (*j);

		return *this;
    }

	/// Scale a vector by multiplication.
	//!
	//! A vector is scaled by multiplying each of its components with the 
	//! factor.
	//!
	//! Through the use of boost::multiplicative as a base class operator * is 
	//! defined as well and implemented in terms of this operator *=.
	//!
	//! \return A reference to this object.
	sgl::vector<D,T> & operator *= (
		/// The scaling factor.
		T const& factor)
	{
		for (T * i=values_; i!=values_+D; ++i)
			(*i) *= factor;

		return *this;
	}

	/// Scale a vector by division.
	//!
	//! A vector is scaled by dividing each of its components through the 
	//! divisor.
	//!
	//! Through the use of boost::multiplicative as a base class operator / is 
	//! defined as well and implemented in terms of this operator /=.
	//!
	//! \return A reference to this object.
	sgl::vector<D,T> & operator /= (
		/// The scaling factor.
		T const& divisor)
	{
		for (T * i=values_; i!=values_+D; ++i)
			(*i) /= divisor;

		return *this;
	}

private:
	// The components of the vector.
	T values_[D];
};


/// A two-dimensional vector with the default component type.
typedef sgl::vector<2> vector2;

/// A two-dimensional vector with integer component type.
typedef sgl::vector<2, int> vector2i;

/// A two-dimensional vector with float component type.
typedef sgl::vector<2, float> vector2f;

/// A two-dimensional vector with double component type.
typedef sgl::vector<2, double> vector2d;


/// A three-dimensional vector with the default component type.
typedef sgl::vector<3> vector3;

/// A three-dimensional vector with integer component type.
typedef sgl::vector<3, int> vector3i;

/// A three-dimensional vector with float component type.
typedef sgl::vector<3, float> vector3f;

/// A three-dimensional vector with double component type.
typedef sgl::vector<3, double> vector3d;


} // namespace sgl

#endif // __sgl_vector_h__
